package com.company.project.search;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.concurrent.TimeUnit;

public class Driver {
    public static PhantomJSDriver getDriver(){
        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl证书支持
        dcaps.setCapability("acceptSslCerts", true);
        //截屏支持
        dcaps.setCapability("takesScreenshot", true);
        //css搜索支持
        dcaps.setCapability("cssSelectorsEnabled", true);
        dcaps.setCapability("phantomjs.page.settings.XSSAuditingEnabled",true);
        dcaps.setCapability("phantomjs.page.settings.webSecurityEnabled",false);
        dcaps.setCapability("phantomjs.page.settings.localToRemoteUrlAccessEnabled",true);
        dcaps.setCapability("phantomjs.page.settings.XSSAuditingEnabled",true);

        dcaps.setCapability("phantomjs.page.settings.loadImages",false);
        //js支持
        dcaps.setJavascriptEnabled(true);
        //驱动支持
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,  System.getProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
        //dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,systemProps.getPhantomjsPath());

        //dcaps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
        //dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 　　Firefox/50.0");
        dcaps.setCapability("ignoreProtectedModeSettings", true);
//        org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
//        proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);
//        proxy.setHttpProxy("http://180.155.128.87:47593/");
//        dcaps.setCapability(CapabilityType.PROXY, proxy);




        //创建无界面浏览器对象
        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
        driver.manage().timeouts().setScriptTimeout(120,TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.manage().deleteAllCookies();
        driver.manage().window().setSize(new Dimension(1920,1080));
        return driver;
    }




    public boolean initOk(WebDriver driver, int CheckTImes, By by){

        int index = CheckTImes;

        while(index > 0){

            try {
                System.out.println(driver.getPageSource());
                WebElement element = driver.findElement(by);
                if(element != null){
                    return true;
                }
            }catch ( Exception e){

            }finally {

            }

            try {
                Thread.sleep(5000);
            }catch (Exception e){

            }

            index --;
        }
        return false;
    }
    public void setProxy(PhantomJSDriver driver){

    }
}
